package com.yc.cn.ycrecycleviewlib.select;

import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;



/**
 * <pre>
 *     @author yangchong
 *     blog  : https://github.com/yangchong211
 *     time  : 2017/05/09
 *     desc  : adapter
 *     revise:
 * </pre>
 */
public abstract class SelectAdapter<VH extends RecyclerView.ViewHolder>
        extends RecyclerView.Adapter<VH> {


    private ArrayList<Integer> selectedIndices;
    private SelectListener selectionListener;
    private int lastCount = -1;
    private int maxSelectionCount = -1;

    private void fireSelectionListener() {
        if (lastCount == selectedIndices.size()) {
            return;
        }
        lastCount = selectedIndices.size();
        if (selectionListener != null) {
            selectionListener.onDragSelectionChanged(lastCount);
        }
    }

    protected SelectAdapter() {
        selectedIndices = new ArrayList<>();
    }

    @CallSuper
    @Override
    public void onBindViewHolder(VH holder, int position) {
        holder.itemView.setTag(holder);
    }

    public void setMaxSelectionCount(int maxSelectionCount) {
        this.maxSelectionCount = maxSelectionCount;
    }

    public void setSelectionListener(SelectListener selectionListener) {
        this.selectionListener = selectionListener;
    }

    public void saveInstanceState(Bundle out) {
        saveInstanceState("selected_indices", out);
    }

    public void saveInstanceState(String key, Bundle out) {
        out.putSerializable(key, selectedIndices);
    }

    public void restoreInstanceState(Bundle in) {
        restoreInstanceState("selected_indices", in);
    }

    public void restoreInstanceState(String key, Bundle in) {
        if (in != null && in.containsKey(key)) {
            selectedIndices = (ArrayList<Integer>) in.getSerializable(key);
            if (selectedIndices == null) {
                selectedIndices = new ArrayList<>();
            } else {
                fireSelectionListener();
            }
        }
    }

    /**
     * 设置选择
     */
    public final void setSelected(int index, boolean selected) {
        if (selected) {
            if (!selectedIndices.contains(index) && (maxSelectionCount == -1
                    || selectedIndices.size() < maxSelectionCount)) {
                selectedIndices.add(index);
                notifyItemChanged(index);
            }
        } else if (selectedIndices.contains(index)) {
            selectedIndices.remove((Integer) index);
            notifyItemChanged(index);
        }
        fireSelectionListener();
    }

    /**
     * 获取某个是否被选中
     */
    public final boolean toggleSelected(int index) {
        boolean selectedNow = false;
        if (selectedIndices.contains(index)) {
            selectedIndices.remove((Integer) index);
        } else if (maxSelectionCount == -1 || selectedIndices.size() < maxSelectionCount) {
            selectedIndices.add(index);
            selectedNow = true;
        }
        notifyItemChanged(index);
        fireSelectionListener();
        return selectedNow;
    }

    /**
     * 选择所有的
     */
    public final void selectAll() {
        int max = getItemCount();
        selectedIndices.clear();
        for (int i = 0; i < max; i++) {
            selectedIndices.add(i);
        }
        notifyDataSetChanged();
        fireSelectionListener();
    }

    /**
     * 清除所有的选择
     */
    public final void clearSelected() {
        selectedIndices.clear();
        notifyDataSetChanged();
        fireSelectionListener();
    }

    /**
     * 获取选择的次数
     */
    public final int getSelectedCount() {
        return selectedIndices.size();
    }

    public final Integer[] getSelectedIndices() {
        return selectedIndices.toArray(new Integer[selectedIndices.size()]);
    }

    /**
     * 是否被选择
     */
    public final boolean isIndexSelected(int index) {
        return selectedIndices.contains(index);
    }
}
